<?php
namespace haibao\comment\business;

abstract class Base{
	const ACCESS_DENIED = 1000;
	const DATA_EMPTY = 10000;
	const DATA_EMPTY_NAME = 10001;
	const DATA_EMPTY_PASSWORD = 10002;
	const DATA_EMPTY_ID = 10003;
	const DATA_EMPTY_USER_GROUP = 10004;
	const DATA_EMPTY_REAL_NAME = 10005;
	const DATA_EMPTY_FUNCTION_CODE =10006;
	const DATA_EMPTY_EVNET_TAG = 10007;
	const DATA_EMPTY_EVENT_KEYWORDS = 10008;
	const DATA_EMPTY_PARENT_ID  = 10009;

	
	//评论管理
	const FUNC_CODE_COMMENT_LIST = 114101;
	const FUNC_CODE_COMMENT_DEL = 114100;
	
	//评论类型
	const FUNC_CODE_COMMENT_TYPE_LIST = 114102;
	const FUNC_CODE_COMMENTTYPE_ADD = 114103;
	const FUNC_CODE_COMMENTTYPE_EDIT = 114104;
	const FUNC_CODE_COMMENTTYPE_DEL = 114105;
	const DATA_ERROR_COMMENT_TYPE_NAME_EXIST = 114900;
	const DATA_ERROR_COMMENT_TYPE_CODE_EXIST = 114901;
	//评论举报
	const FUNC_CODE_COMMENT_REPORT_LIST = 114106;
	//冻结评论
	const FUNC_CODE_COMMENT_BAN_LIST = 114107;
	
	//评论审核
	const FUNC_CODE_COMMENT_VERIFY = 114108;
	//敏感词
	const FUNC_CODE_COMMENT_SENSITIVE = 114109;
	//冻结管理
	const FUNC_CODE_COMMENT_BAN_ADD = 115101;
	
	//灌水管理
	const FUNC_CODE_COMMENT_VEST = 116101;

	
	const DATA_ERROR_CODE_SYSTEM = 40001;//系统错误
	
	// 设置评论来源
	const COMMENT_FROM_ARTICLE = 1;
	const COMMENT_FROM_LIST = 2;
	private static $currentUser = null;
	
	public function __construct(){}
	

	public static function getImageLink($imgUrl,$width = 600,$height = 0,$quality = 100,$water = 0){
	
		$url = '';
		$key = \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_IMG_ENCRYPT_KEY);
		$imgFileName = substr($imgUrl, strrpos($imgUrl, '/')+1);
		$imgName = substr($imgFileName, 0, strrpos($imgFileName, '.'));
		$imgType = substr($imgFileName, strrpos($imgFileName, '.'));
		$condition = '/'.$width.'_'.$height.'_'.$quality.'_'.$water.'/'.$imgName.'/';
		$newUrl = '';
		$isHasUploads = strpos($imgUrl, \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::IMAGE_UPLOAD_BASE_PATH));
		if($isHasUploads === false){
			$config = \haibao\comment\Config::getFrameConfig();
			$newImgName = str_replace($config[\haibao\frame\http\HttpEnvironment::DOMAIN], '', $imgUrl);
			$newUrl = \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_CDN_DOMAIN).'/img'.$condition.$newImgName;
		} else {
			$newImgName = md5($condition.$key);
			$newUrl = \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_CDN_DOMAIN).'/img'.$condition.$newImgName.$imgType;
		}
	
		if(\haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_CDN_DEFAULT)){
			$newImgName = md5($newUrl.$key);
			$hasCount = strpos($newUrl, \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_CDN_DOMAIN));
	
			$cdnNum = self::getHash($newImgName);
			$imgUrl = \haibao\comment\Config::$cdnNewHost[$cdnNum];
			$newUrl = str_replace(\haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_CDN_DOMAIN).'/img',$imgUrl,$newUrl);
	
		}
		return $newUrl;
	}
	
	/**
	 * 得导航面包屑
	 */
	public static function getCurrentPos($primaiyTagId){

		$tagParentModels = \haibao\cms\business\Tag::getParentsTagByIds(array($primaiyTagId));
		$tagList = array();
		$togTagId = 0;
		if($tagParentModels && $tagParentModels[$primaiyTagId]){
			$TopTag = array('Url' =>'','TagName' =>'');
			$tagParentModels = array_reverse($tagParentModels[$primaiyTagId],true);
			$isStructureTag = false;
			foreach($tagParentModels as $key=>$tagModel){
				$tagModel->Url = \haibao\cms\business\control\Base::getTagUrl($tagModel->Url, $tagModel->TagType,$tagModel->TopId,$tagModel->Id);
				$tagList[$key]['TagName'] = $tagModel->TagName;
				$tagList[$key]['Url'] = $tagModel->Url;
				$togTagId = $tagModel->TopId;
	
			}
		}
		return array($tagList,$togTagId);
	}
	
	private static function getHash($key,$isNew = true){
		$hash = crc32($key) >> 16 & 0xffff;
		$cdnHostArr = \haibao\comment\Config::$cdnNewHost;
		if(!$isNew){
			$cdnHostArr = \haibao\comment\Config::$cdnHost;
		}
		return sprintf('%01s',$hash % count($cdnHostArr));
	}
	
}
